import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from "../store"

Vue.use(VueRouter)

  const routes = [
  {
      path: '/',
      redirect: "/list"
  },
  {
    path: '/login',
      name:"login",
    component: () => import('../views/login.vue')
  },
    {
      path: '/list',
      component: () => import('../views/list.vue')
    },
    {
      path: '/create',
      component: () => import('../views/create.vue')
    },
    {
      path: "/deal",
      component: () => import('../views/deal.vue')
    },
    {
      path: "/next",
      component: () => import('../views/next.vue'),
      meta:{
        keepAlive: true
      }
    },

    {
      path: "/form_setting",
      name:"form-setting",
      component: () => import('../views/form_setting.vue')
    },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  console.log(to)
  if (to.name === 'form-setting') next()
  else if (to.name !== 'login' && !store.getters.isLogin) next({ name: 'login' })
  else next()
})

import Watermark from '../lib/watermark';
router.afterEach((to) => {
  try {
    let user = JSON.parse(localStorage.getItem("$user"));
    Watermark.set((user["trueName"] || user.true_name) + user.id);
  }catch (e) {
    Watermark.set(SYSTEM_ENV.sys_name);
  }
});

export default router
